package property.controller;

import com.alibaba.fastjson.JSONObject;
import property.pojo.*;
import property.service.PeopleService;
import property.service.QueryService;
import property.service.impl.PeopleServiceImpl;
import property.service.impl.QueryServiceImpl;
import property.utils.servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = {"/PeopleManager/*"})
public class PeopleServlet extends BaseServlet {

    private PeopleService peopleService = new PeopleServiceImpl();

    private void applyRegister(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();

        //数据发送简易身份验证
        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");

        //0 -- 用户  1 -- 员工  2 -- 管理员
        short registerType = jb.getShort("type");
        JSONObject jbInsert = jb.getJSONObject("insert");
        int i = 0;

        if(registerType == 0){
            User user = JSONObject.toJavaObject(jbInsert, User.class);
            i = peopleService.applyRegister(user);
        } else if (registerType == 1) {
            if(character == 1){
                jbReturn.put("code",0);
                jbReturn.put("messages","权限不足！！");
                response.getWriter().write(jbReturn.toString());
                return;
            }
            Staff staff = JSONObject.toJavaObject(jbInsert,Staff.class);
            i = peopleService.applyRegister(staff);
        } else {
            if(character == 2 && ((Admin) session.getAttribute("currentUser")).getaPermission() == 0){
                jbReturn.put("code",0);
                jbReturn.put("messages","权限不足！！");
                response.getWriter().write(jbReturn.toString());
                return;
            }
            Admin admin = JSONObject.toJavaObject(jbInsert, Admin.class);
            i = peopleService.applyRegister(admin);
        }
        if(i > 0){
            jbReturn.put("code",1);
            jbReturn.put("messages","新增数据成功");
        }else if(i == -2){
            jbReturn.put("code",-2);
            jbReturn.put("messages","该手机号账号已存在！！！");
        } else {
            jbReturn.put("code",0);
            jbReturn.put("messages","新增数据失败");
        }
        response.getWriter().write(jbReturn.toString());
    }

    public void deregister(HttpServletRequest request,HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();
        int targetType = jb.getShort("type");
        int id = jb.getInteger("deregisterId");


        //数据发送简易身份验证
        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");
        if(targetType == 1 && character == 1){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }else if (targetType == 2 && character == 2 && ((Admin) session.getAttribute("currentUser")).getaPermission() == 0) {
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }
        int i = peopleService.deregister(targetType,id);
        if(i > 0){
            jbReturn.put("code",1);
            jbReturn.put("messages","删除成功");
        }else if (i < 0){
            jbReturn.put("code",0);
            jbReturn.put("messages","关联数据未删除！删除失败");
        }else{
            jbReturn.put("code",0);
            jbReturn.put("messages","删除失败");
        }
        response.getWriter().write(jbReturn.toString());
    }

    public void updatePeopleData(HttpServletRequest request,HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbUpdate = JSONObject.parseObject(jb.getString("update"));
        JSONObject jbReturn = new JSONObject();

        //数据发送简易身份性验证
        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");

        //0 -- 用户  1 -- 员工  2 -- 管理员
        byte updateType = jb.getByte("type");
        int i = 0;

        if(updateType == 0){
            User user = JSONObject.toJavaObject(jbUpdate, User.class);
            i = peopleService.updatePeopleData(user);
        } else if (updateType == 1) {
            if(character == 1){
                jbReturn.put("code",0);
                jbReturn.put("messages","权限不足！！");
                response.getWriter().write(jbReturn.toString());
                return;
            }
            Staff staff = JSONObject.toJavaObject(jbUpdate, Staff.class);
            i = peopleService.updatePeopleData(staff);
        } else {
            if(character == 2 && ((Admin) session.getAttribute("currentUser")).getaPermission() == 0){
                jbReturn.put("code",0);
                jbReturn.put("messages","权限不足！！");
                response.getWriter().write(jbReturn.toString());
                return;
            }
            Admin admin = JSONObject.toJavaObject(jbUpdate, Admin.class);
            i = peopleService.updatePeopleData(admin);
        }
        if(i > 0){
            jbReturn.put("code",1);
            jbReturn.put("messages","修改成功");
            System.out.println(1);
        } else if(i == -2){
            jbReturn.put("code",-2);
            jbReturn.put("messages","该手机号账号已存在！！！");
            System.out.println("手机号存在");
        }  else{
            jbReturn.put("code",0);
            jbReturn.put("messages","修改失败");
        }
        response.getWriter().write(jbReturn.toString());
    }
}
